package com.yutianran.mark.model;

import android.nfc.Tag;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Author：Yutianran on 2018/9/pr30 下午9:15
 */
public class Item implements Comparable<Item> {

    private Item parent;//父Item
    private List<Item> childs;//子Item列表
    private double order;//排序值
    private String title;//标题

    private List<Tag> tags;//标签列表

    private CompleteState completeState;//完成状态
    private StarState starState;//星标状态

    private String link;//内链
    private String content;//详细内容

    private long createTime;//创建时间
    private long updateTime;//更新时间
    private long completeTime;//完成时间
    private long deadline;//截止时间

    private int level;//级别
    private Project project;//项目
    private long value;//项目数值

    public Item() {
        this.level = 0;
        this.title = "root";
        this.childs = new ArrayList<>();
    }

    public Item(String title) {
        this.title = title;
        this.createTime = System.currentTimeMillis();
        this.updateTime = System.currentTimeMillis();
        this.childs = new ArrayList<>();
    }

    /**
     * 添加子项
     *
     * @param item
     */
    public void addChild(Item item) {
        ObjectHelper.requireNotNull(item);
        if (childs.isEmpty()) {
            item.setOrder(100.0d);
        } else {
            item.setOrder(childs.get(childs.size() - 1).getOrder() + 100.0d);
        }
        childs.add(item);
        item.setParent(this);
        item.level = this.level + 1;
    }

    /**
     * 添加下一项
     *
     * @param item
     */
    public void addNext(Item item) {
        ObjectHelper.requireNotNull(item);
        item.setOrder(this.getOrder() + 100.0d);
        parent.childs.add(item);
        item.setParent(parent);
        item.level = this.level;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public void setUpdateTime(long updateTime) {
        this.updateTime = updateTime;
    }

    public void setParent(Item parent) {
        this.parent = parent;
    }

    public double getOrder() {
        return order;
    }

    public void setOrder(double order) {
        this.order = order;
    }

    @Override
    public int compareTo(@NonNull Item item) {
        double var = this.getOrder() - item.getOrder();
        if (var == 0) {
            throw new RuntimeException("排序冲突");
        }
        return var > 0 ? 1 : -1;
    }

    public void print(String tag) {
        System.out.println(String.format("---------------[%s]----------------", tag));
        print();
    }

    public void print() {
        Collections.sort(getChilds());
        System.out.println(getLevelTitle());
        for (Item child : childs) {
            child.print();
        }
    }

    private String getLevelTitle() {
        return getLevelChar() + title;
    }

    private String getLevelChar() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < level; i++) {
            stringBuilder.append("-");
        }
        return stringBuilder.toString();
    }

    public List<Item> getChilds() {
        return childs;
    }

    public Item getParent() {
        return parent;
    }

    /**
     * 拖动到指定位置
     *
     * @param item
     */
    public void dragToPosition(Item item) {
        List<Item> childs = item.getParent().getChilds();
        int position = childs.indexOf(item);
        double currentOrder = item.getOrder();
        double previousOrder = 0.0d;
        if (position > 0) {
            previousOrder = childs.get(position - 1).getOrder();
        }
        order = (currentOrder - previousOrder) / 2 + previousOrder;
        getParent().getChilds().remove(this);
        childs.add(this);
        this.level = item.level;
    }
}
